package com.googlecode.curvedmetromaps.core.logic.forcedriven;

import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;

import com.googlecode.curvedmetromaps.core.logic.costfunction.CostConstants;
import com.googlecode.curvedmetromaps.core.model.MetroStation;

/**
 * Constraint S3: forces that push vertices apart.
 */
public class ForceRepulsiveStations extends ForceSubsystem {

	ForceRepulsiveStations(ForceSystem system) {
		super(system);
		baseWeight.value = 3000;
	}

	void applyForces(int delta) {
		for (int m1 = 0; m1 < system.stationBodies.length; m1++) {
			for (int m2 = m1 + 1; m2 < system.stationBodies.length; m2++) {
				Body m1body = system.stationBodies[m1];
				Body m2body = system.stationBodies[m2];
				Vec2 p1 = m1body.getPosition();
				Vec2 p2 = m2body.getPosition();
				
				if (system.metroMap.stations[m1].ref.equals("244494496") && system.metroMap.stations[m2].ref.equals("324363999")) {
					p1.mul(5f);
				}

				Vec2 direction = p1.sub(p2);
				float len = direction.normalize();
				if (len == 0) {
					MetroStation ms1 = (MetroStation) m1body.getUserData();
					MetroStation ms2 = (MetroStation) m2body.getUserData();
					direction = new Vec2(ms2.geoX - ms1.geoX, ms2.geoY - ms1.geoY);
					len = 1;
				}

				double f = baseWeight.value / (len * len);

				direction.mulLocal((float) f);

				m1body.applyForceToCenter(direction);
				m2body.applyForceToCenter(direction.negate());
			}
		}
	}
}
